package com.bb.blog.code.mybatis.mapper;

import org.springframework.beans.factory.FactoryBean;

import java.lang.reflect.Proxy;

public class MapperXFactoryBean<T> implements FactoryBean<T> {

    private Class<T> mapperInterface;

    public MapperXFactoryBean(Class<T> mapperInterface) {
        this.mapperInterface = mapperInterface;
    }

    @Override
    @SuppressWarnings("unchecked")
    public T getObject() throws Exception {
        T mapperX = (T) Proxy.newProxyInstance(
            this.getClass().getClassLoader(),
            new Class[]{mapperInterface},
            new MapperXProxy<>(mapperInterface)
        );
        MapperXRegistry.addMapper(mapperInterface, mapperX);
        return mapperX;


    }

    @Override
    public Class<?> getObjectType() {
        return mapperInterface;
    }
}
